#include "characterselectdialog.h"
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QMessageBox>

CharacterSelectDialog::CharacterSelectDialog(QWidget* parent)
    : QDialog(parent)
{
    setWindowTitle("选择角色");
    setFixedSize(1000, 500);
    setStyleSheet(
        "QDialog {"
        "    border-image: url(:/herobg.png) 0 0 0 0 stretch stretch;"
        "}"
        "#centralWidget > * {"
        "    background: transparent;"
        "}"
        );
    setupUI();
}

void CharacterSelectDialog::setupUI()
{
    // 主布局
    QVBoxLayout* mainLayout = new QVBoxLayout(this);
    mainLayout->setAlignment(Qt::AlignCenter);

    // 标题
    QLabel* title = new QLabel("选择你的英雄", this);
    title->setStyleSheet(
        "font-size: 48px;"
        "color: white;"
        "font-weight: bold;"
        );
    title->setAlignment(Qt::AlignCenter);

    // 角色选择布局
    QHBoxLayout* charLayout = new QHBoxLayout;
    m_buttonGroup = new QButtonGroup(this);
    charLayout->setSpacing(30);
    charLayout->setContentsMargins(20, 0, 20, 0);

    // 角色按钮
    const QStringList characters = {
        ":/char1.png",
        ":/char2.png",
        ":/char3.png",
        ":/char4.png"
    };

    for (int i = 0; i < characters.size(); ++i) {
        QPushButton* btn = new QPushButton(this);
        btn->setCheckable(true);
        btn->setFixedSize(200, 300);

        QString style = QString(
                            "QPushButton {"
                            "    border-image: url(%1);"
                            "    border: 2px solid transparent;"
                            "}"
                            "QPushButton:checked {"
                            "    border: 3px solid #FFD700;"
                            "}"
                            ).arg(characters[i]);

        btn->setStyleSheet(style);
        m_buttonGroup->addButton(btn, i);
        charLayout->addWidget(btn);
    }

    // 确认按钮
    QPushButton* confirmBtn = new QPushButton("开始冒险", this);
    confirmBtn->setStyleSheet(
        "QPushButton {"
        "    background-color: #4CAF50;"
        "    color: white;"
        "    font-size: 24px;"
        "    padding: 15px 30px;"
        "    border-radius: 10px;"
        "}"
        "QPushButton:hover {"
        "    background-color: #45a049;"
        "}"
        );

    connect(confirmBtn, &QPushButton::clicked, [this]() {
        if (m_buttonGroup->checkedId() == -1) {
            QMessageBox::warning(this, "提示", "请先选择一个角色！");
            return;
        }
        m_selectedId = m_buttonGroup->checkedId();
        accept();
    });

    // 组合布局
    mainLayout->addWidget(title);
    mainLayout->addLayout(charLayout);
    mainLayout->addWidget(confirmBtn, 0, Qt::AlignCenter);
}
